package com.pb.service.boot.config;

import com.pb.infra.auth.entity.custom.PriviUserCustom;
import com.pb.infra.oth.util.UserInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * security用户认证
 * @author liudebao
 */
@Service
public class UserDetailService implements UserDetailsService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public UserDetails loadUserByUsername(String token) throws UsernameNotFoundException {
        PriviUserCustom userInfo = new PriviUserCustom();
        UserInfo user = (UserInfo)redisTemplate.opsForValue().get(token);
        userInfo.setUserId(user.getUserId());
        userInfo.setUserName(user.getUserName());
        userInfo.setRoles(user.getUserRoles());
        return userInfo;
    }
}
